/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.pms.service;

import com.fzhucc.exception.BadRequestException;
import com.fzhucc.modules.pms.domain.ProjectLog;
import com.fzhucc.modules.pms.domain.ProjectService;
import com.fzhucc.modules.pms.domain.dto.*;
import com.fzhucc.modules.pms.repository.ProjectRepository;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.pms.repository.ProjectLogRepository;
import com.fzhucc.modules.pms.service.mapstruct.ProjectLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2022-12-14
**/
@Service
@RequiredArgsConstructor
public class ProjectLogService {

    private final ProjectLogRepository projectLogRepository;
    private final ProjectLogMapper projectLogMapper;
    private final ProjectRepository projectRepository;

    public Map<String,Object> queryAll(ProjectLogQueryCriteria criteria, Pageable pageable){
        Page<ProjectLog> page = projectLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        Page<ProjectLogDto> map = page.map(projectLogMapper::toDto);
        List<ProjectLogDto> list = map.getContent();
        for (int i = 0; i < list.size(); i++) {
            ProjectLogDto projectLogDto = list.get(i);
            projectLogDto.setProject(projectRepository.findById(projectLogDto.getProjectId()).get());
        }
        return PageUtil.toPage(map);
    }

    public List<ProjectLogDto> queryAll(ProjectLogQueryCriteria criteria){
        return projectLogMapper.toDto(projectLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    public ProjectLogDto findById(Integer id) {
        ProjectLog projectLog = projectLogRepository.findById(id).orElseGet(ProjectLog::new);
        ValidationUtil.isNull(projectLog.getId(),"ProjectLog","id",id);
        return projectLogMapper.toDto(projectLog);
    }

    @Transactional(rollbackFor = Exception.class)
    public ProjectLogDto create(ProjectLog resources) {
        return projectLogMapper.toDto(projectLogRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(ProjectLog resources) {
        ProjectLog projectLog = projectLogRepository.findById(resources.getId()).orElseGet(ProjectLog::new);
        ValidationUtil.isNull( projectLog.getId(),"ProjectLog","id",resources.getId());
        projectLog.copy(resources);
        projectLogRepository.save(projectLog);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            projectLogRepository.deleteById(id);
        }
    }

    public void download(List<ProjectLogDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProjectLogDto projectLog : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("项目id", projectLog.getProjectId());
            map.put("描述", projectLog.getDescribe());
            map.put("变更时间", projectLog.getChangeTime());
            map.put("变更前等级", projectLog.getBeforeLevel());
            map.put("变更后等级", projectLog.getAfterLevel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    public Map<String,Object> queryProjectLogByProjectId(ProjectLogWithProjectIdDto projectLogWithProjectIdDto, Pageable pageable){
        String projectId = projectLogWithProjectIdDto.getProjectId();
        if (projectId==null || projectId.equals("")){
            throw new BadRequestException("未找到项目ID");
        }
        Page<ProjectLog> page = projectLogRepository.findAllByProjectId(projectId, pageable);
        return PageUtil.toPage(page.map(projectLogMapper::toDto));
    }
}